<?php
namespace app\modules\merchant\services;

use app\common\services\BaseService;
use app\utils\UserUtil;

class ConfigService extends BaseService{

    /**
     * 公告
     * @param $content
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function announce($content){
        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->announce = $content;
        return $merchant->update() !== false;
    }

    /**
     * 自动接单
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function autoAccept(){
        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->auto_accept = ($merchant->auto_accept % 2) + 1;
        return $merchant->update() !== false;
    }

    /**
     * 配送方式
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function delivery(){
        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->delivery_type = ($merchant->delivery_type % 2) + 1;
        return $merchant->update() !== false;
    }

    /**
     * 配送规则 公里数-金额
     * @param $rules
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function deliveryRule($rules){
        $rules = str_replace('；',';',$rules);
        foreach (explode(";",$rules) as $rule){
            if(substr_count($rule,"-") >= 2){
                self::setError(\Yii::t("app","规则格式错误"));
                return false;
            }
            $temp = explode("-",$rule);
            if(is_numeric($temp[0]) === false || is_numeric($temp[1]) === false){
                self::setError(\Yii::t("app","规则格式错误"));
                return false;
            }
        }
        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->delivery_rule = $rules;
        return $merchant->update() !== false;
    }

    /**
     * 显示
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function show(){
        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->is_show = ($merchant->is_show % 2) + 1;
        return $merchant->update() !== false;
    }

    /**
     * 营业时间
     * @param $time
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     */
    public static function businessTime($time){
        if(empty($time)){
            self::setError(\Yii::t("app","营业时间不能为空"));
            return false;
        }
        if(strpos($time,"-") === false){
            self::setError(\Yii::t("app","营业时间格式错误"));
            return false;
        }
        $bus_time = explode("-",$time);
        if(count($bus_time) > 2){
            self::setError(\Yii::t("app","营业时间格式错误"));
            return false;
        }
        if(!is_numeric($bus_time[0]) || !is_numeric($bus_time[1])){
            self::setError(\Yii::t("app","营业时间格式错误"));
            return false;
        }
        if($bus_time[0] > $bus_time[1]){
            $temp = $bus_time[0];
            $bus_time[0] = $bus_time[1];
            $bus_time[1] = $temp;
        }

        $merchant = UserUtil::getModuleUserIdentity("merchant");
        $merchant->business_time= $time;
        return $merchant->update() !== false;
    }
}